home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / vstsrc / satdb.c < prev    next >
C/C++ Source or Header  |  1995-01-28  |  13KB  |  339 lines

  1. /*
  2.  * %W% %E% %U%  [EXTREL_1.2]
  3.  *
  4.  * VersaTrack orbit calculations are based on those that appear in Dr. Manfred
  5.  * Bester's sattrack program (the Unix(tm) versions 1 and 2).
  6.  *
  7.  * The data from which the maps where generated come from "xsat", an
  8.  * X-Windows program by David A. Curry (N9MSW).
  9.  *
  10.  * Site coordinates come from various sources, including a couple of
  11.  * World Almanacs, and also from both of the programs mentioned above.
  12.  *
  13.  * The following are authors' applicable copyright notices:
  14.  *
  15.  *                                                                               
  16.  * Copyright (c) 1992, 1993, 1994 Manfred Bester. All Rights Reserved.        
  17.  *                                                                           
  18.  * Permission to use, copy, modify, and distribute this software and its      
  19.  * documentation for educational, research and non-profit purposes, without   
  20.  * fee, and without a written agreement is hereby granted, provided that the  
  21.  * above copyright notice and the following three paragraphs appear in all    
  22.  * copies.                                                                    
  23.  *                                                                              
  24.  * Permission to incorporate this software into commercial products may be    
  25.  * obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,     
  26.  * Berkeley, CA 94709, USA.                                                   
  27.  *                                                                             
  28.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,  
  29.  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF    
  30.  * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED   
  31.  * OF THE POSSIBILITY OF SUCH DAMAGE.                                         
  32.  *                                                                             
  33.  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT       
  34.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A    
  35.  * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"       
  36.  * BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,  
  37.  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                   
  38.  *                                                                             
  39.  *                                                                             
  40.  * Copyright 1992 by David A. Curry                                            
  41.  *                                                                             
  42.  * Permission to use, copy, modify, distribute, and sell this software and its 
  43.  * documentation for any purpose is hereby granted without fee, provided that  
  44.  * the above copyright notice appear in all copies and that both that copyright
  45.  * notice and this permission notice appear in supporting documentation.  The  
  46.  * author makes no representations about the suitability of this software for  
  47.  * any purpose.  It is provided "as is" without express or implied warranty.   
  48.  *                                                                             
  49.  * David A. Curry, N9MSW                                                       
  50.  * Purdue University                                                           
  51.  * Engineering Computer Network                                                
  52.  * 1285 Electrical Engineering Building                                        
  53.  * West Lafayette, IN 47907                                                    
  54.  * davy@ecn.purdue.edu                                                         
  55.  *                                                                             
  56.  * VersaTrack Copyright (c) 1993, 1994 Siamack Navabpour. All Rights Reserved.
  57.  *
  58.  * Permission is hereby granted to copy, modify and distribute VersaTrack
  59.  * in whole, or in part, for educational, non-profit and non-commercial use
  60.  * only, free of charge or obligation, and without agreement, provided that
  61.  * all copyrights and restrictions noted herein are observed and followed, and
  62.  * additionally, that this and all other copyright notices listed herein
  63.  * appear unaltered in all copies and in all derived work.
  64.  *
  65.  * This notice shall not in any way void or supersede any of the other authors
  66.  * rights or privileges.
  67.  *
  68.  * VersaTrack IS PRESENTED FREE AND "AS IS", WITHOUT ANY WARRANTY OR SUPPORT.
  69.  * YOU USE IT AT YOUR OWN RISK. The author(s) shall not be liable for any
  70.  * direct, indirect, incidental, or consequential damage, loss of profits or
  71.  * other tangible or intangible losses or benefits, arising out of or related
  72.  * to its use. VersaTrack carries no warranty, explicit or implied, including
  73.  * but not limited to those of merchantablity and fitness for a particular
  74.  * purpose.
  75.  *
  76.  * Siamack Navabpour, 12342 Hunter's Chase Dr. Apt. 2114, Austin, TX 78729.
  77.  * sia@bga.com or sia@realtime.com.
  78.  */
  79.  
  80.  
  81. #include <windows.h>
  82. #include <math.h>
  83. #include <stdio.h>
  84. #include <string.h>
  85. #include <stdlib.h>
  86.  
  87. #include "vstdefs.h"
  88. #include "vsttype.h"
  89. #include "libxtrns.h"
  90.  
  91. static int nsats;
  92. static satellite_t *Snip, **satinfop;
  93.  
  94. static double
  95. getElement(gstring,gstart,gstop)
  96. int  gstart, gstop;
  97. char gstring[80];
  98. {
  99.     int  k, glength;
  100.     char gstr[74];
  101.  
  102.     glength = gstop - gstart + 1;
  103.  
  104.     for (k = 0; k <= glength; k++)
  105.         gstr[k] = gstring[gstart+k-1];
  106.  
  107.     gstr[glength] = '\0';
  108.  
  109.     return atof(gstr);
  110. }
  111.  
  112. /*
  113.  * Read in the orbital elements.
  114.  */
  115.  
  116. satellite_t **
  117. readSatDB(int version, char *datadir, char *satfile, char *tbuf)
  118. {
  119.     FILE *fp;
  120.     HANDLE hg;
  121.     satellite_t *si, *dup, *last, **satinfop;
  122.     double exp;
  123.     int    i, j, error, index, linenum, checkSum, checkValue;
  124.     char   line0buf[81], line1buf[81], line2buf[81], str[81], strng[10];
  125.     char   *line0, *line1, *line2;
  126.     static char string[200];
  127.  
  128.     extern int satcmp(const void *, const void *);
  129.     extern satellite_t *findSatbyCatnum(int);
  130.  
  131.     sprintf(tbuf, "%s\\%s", datadir, satfile);
  132.     if ((fp = fopen(tbuf, "r")) == NULL) {
  133.         sprintf(string,"Cannot open \"%s\"", tbuf);
  134.         strncpy(tbuf, string, 80);
  135.         return NULL;
  136.     }
  137.     index = 0;
  138.     linenum = 0;
  139.     Snip = NULL;        
  140.     while ((line0 = getline(line0buf, sizeof(line0buf), fp)) != NULL) {
  141.         linenum++;
  142.         if (line0[0] == 0 || line0[0] == '#') /* skip comment lines */
  143.             continue;
  144.         do {
  145.             linenum++;
  146.             if ((line1 = getline(line1buf, sizeof(line1buf), fp)) == NULL) {
  147.                 sprintf(tbuf,"Premature eof on satellite file %s line %d (element set line 1)\n",
  148.                         satfile, linenum);
  149.                 return NULL;
  150.             }
  151.         } while (!line1[0] || line1[0] == '\n' || line1[0] == '#');
  152.         
  153.         do {
  154.             linenum++;
  155.             if ((line2=getline(line2buf, sizeof(line2buf), fp)) == NULL) {
  156.                 sprintf(tbuf, "Premature eof on satellite file %s line %d (element set line 2)\n",
  157.                         satfile, linenum);
  158.                 return NULL;
  159.             }
  160.         } while (!line2[0] || line2[0] == '\n' || line2[0] == '#');
  161.  
  162.         error = 0;
  163.         for (j = 2; j <= 3; j++) {       /* j = error code */
  164.             if (j == 2)
  165.                 strcpy(str, line1);
  166.             else
  167.                 strcpy(str, line2);
  168.  
  169.             checkSum = 0;
  170.  
  171.             for (i = 0; i < 68; i++) {
  172.                 strng[0]   = str[i];
  173.                 strng[1]   = '\0';
  174.                 checkValue = atoi(strng);
  175.  
  176.                 if (!strcmp(strng,"-"))
  177.                     checkValue = 1;      /* assign check sum value to '-' */
  178.  
  179.                  checkSum += checkValue;
  180.             }
  181.  
  182.             strng[0] = str[68];
  183.             strng[1] = '\0';
  184.  
  185.             if (atoi(strng) != (checkSum % 10))  {
  186.                 sprintf(tbuf, "%s: around line %d: %s elset line %d checksum is %d, should be %d",
  187.                     satfile, linenum, line0, j-1, checkSum%10, atoi(strng));
  188.                 error = j;
  189.             }
  190.         }
  191.         if (error)
  192.             continue;
  193.  
  194.         i = (long) getElement(line1, 3,   8);    /* Catalogue No. */
  195.         j = (long) getElement(line1, 65, 68);    /* Eleset No. */
  196.         dup = findSatbyCatnum(i);
  197.         hg = NULL;
  198.  
  199.         if (dup) {
  200.             if (cistrcmp(line0, dup->s_name)) {
  201.                 sprintf(tbuf, "Object # %d has different names %s and %s",
  202.                         i, dup->s_name, line0);
  203.                 usermsg(NULL, tbuf);
  204.             }
  205.             if ((long) j <= (long) dup->s_elementset)
  206.                 continue;
  207.                 si = dup;
  208.         }
  209.         else {
  210.                if (!Snip) {
  211.                 if (!(hg = GlobalAlloc(GPTR, sizeof(satellite_t)))) {
  212.                     sprintf(tbuf,"%s: out of memory at line %d", satfile, linenum);
  213.                     usermsg(NULL, tbuf);
  214.                     break;
  215.                 }
  216.                 Snip = (satellite_t *) GlobalLock(hg);
  217.                 si = Snip;
  218.             }
  219.             else {
  220.                 si = last;
  221.                 if (!(hg = GlobalAlloc(GPTR,sizeof(satellite_t)))) {
  222.                     sprintf(tbuf,"%s: out of memory at line %d", satfile, linenum);
  223.                     usermsg(NULL, tbuf);
  224.                     break;
  225.                 }
  226.                 si->s_next = (satellite_t *) GlobalLock(hg);
  227.                 si = si->s_next;
  228.               }
  229.             if (si == NULL) {
  230.                 sprintf(tbuf, "%s: out of memory at line %d", satfile, linenum);
  231.                 usermsg(NULL, tbuf);
  232.                 break;
  233.             }
  234.             nsats++;
  235.             si->s_lbid  = index++;
  236.             si->s_next  = NULL;
  237.             si->s_modep = NULL;
  238.             si->s_flags = 0;
  239.             strncpy(si->s_name, line0, sizeof(si->s_name)-1);
  240.             last = si;
  241.         }
  242.  
  243.         si->s_satnum        = (long) i;
  244.         si->s_epochyear     = getElement(line1, 19, 20);
  245.         si->s_epochtime     = getElement(line1, 21, 32);
  246.         si->s_decayrate     = getElement(line1, 34, 43);
  247.         si->s_ephemeristype = getElement(line1, 63, 63);
  248.         exp                 = getElement(line1, 51, 52);
  249.         si->s_dratedot      = getElement(line1, 45, 50) * pow(10.0, -5.0 + exp);
  250.         exp                 = getElement(line1, 60, 61);
  251.         si->s_bstarcoeff    = getElement(line1, 54, 59) * pow(10.0, -5.0 + exp);
  252.         si->s_elementset    = (long) j;
  253.         si->s_inclination   = getElement(line2,  9, 16);
  254.         si->s_raan          = getElement(line2, 18, 25);
  255.         si->s_eccentricity  = getElement(line2, 27, 33) * 1.0e-7;
  256.         si->s_argperigee    = getElement(line2, 35, 42);
  257.         si->s_meananomaly   = getElement(line2, 44, 51);
  258.         si->s_meanmotion    = getElement(line2, 53, 63);
  259.         si->s_orbitnum      = (long) getElement(line2, 64, 68);
  260.         si->s_epochtime    += day_number((int)si->s_epochyear, 1, 0);
  261.         UTCdateStr(si->s_epochtime, si->s_epochstr);
  262.         if (hg)
  263.             GlobalUnlock(hg);
  264.     }
  265.     fclose(fp);
  266.  
  267.     if (!(hg = GlobalAlloc(GPTR, (index+1) * sizeof(satellite_t *)))) {
  268.         sprintf(tbuf, "%s: out of memory", satfile);
  269.         usermsg(NULL, tbuf);
  270.         return NULL;
  271.     }
  272.     satinfop = (satellite_t **) GlobalLock(hg);
  273.     
  274.     /* Eliminate duplicate names and build indirect vector */
  275.     
  276.     for (index = 0, si = Snip; si ; si = si->s_next) {
  277.         for (last = si->s_next; last; last = last->s_next) {
  278.             if (cistrcmp(si->s_name, last->s_name) == 0)
  279.                 goto next;
  280.         }
  281.         satinfop[index++] = si;
  282.        next: ;
  283.     }
  284.     satinfop[index] = (satellite_t *) 0;
  285.     qsort(satinfop, (unsigned) index, sizeof(satellite_t *), satcmp);
  286.     GlobalUnlock(hg);
  287.  
  288.     return satinfop;
  289. }
  290.  
  291. satellite_t *
  292. findSatbyName(sinfp, buf)
  293. satellite_t **sinfp;
  294. char *buf;
  295. {
  296.     register satellite_t **sip;
  297.  
  298.     if (!sinfp)
  299.         return NULL;
  300.  
  301.     for (sip = sinfp; *sip; sip++)
  302.         if (cistrcmp((*sip)->s_name, buf) == 0)
  303.             return *sip;
  304.  
  305.     return (satellite_t *) 0;
  306. }
  307.  
  308. static
  309. satellite_t *
  310. findSatbyCatnum(num)
  311. int num;
  312. {
  313.     register satellite_t *sip;
  314.  
  315.     for (sip = Snip; sip; sip = sip->s_next)
  316.         if (sip->s_satnum == num)
  317.             return sip;
  318.  
  319.     return (satellite_t *) 0;
  320. }
  321.  
  322. static
  323. satellite_t *
  324. findSatbyID(id)
  325. register int id;
  326. {
  327.     if (!satinfop || id >= nsats || id < 0)
  328.         return NULL;
  329.  
  330.     return satinfop[id];
  331. }
  332.  
  333. static int
  334. satcmp(a, b)
  335. const void *a, *b;
  336. {
  337.     return cistrcmp((*(satellite_t **)a)->s_name, (*(satellite_t **)b)->s_name);
  338. }
  339.